/**
 *	@author: Manimin R. Morga
 *	date created: June 4, 2006
 *	version: 2.0
 */

package text.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBConnector {
	
	private static Connection con; 
	
	public DBConnector()	{	
		con = null;		
	}
		
	public static void connect() {
		try {
			String userName = "root";
			String password = "";
			String url = "jdbc:mysql://localhost:3306/TExtDB";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			con = DriverManager.getConnection (url, userName, password);
		}catch(Exception e) {
			System.out.println("Cannot connect to DB");
		}
	}
	
	public static ResultSet select(String sQuery)	{
		try{
		//	System.out.println(sQuery);
			Statement stat = con.createStatement();
			ResultSet rs = stat.executeQuery(sQuery);
			return rs;
		}catch(Exception e)	{
			e.printStackTrace();
			System.out.println(sQuery);
			System.out.println("Select Error!");
		}
		return null; 
	}
	
	public static void insert(String sQuery)	{
		try	{
			Statement stat = con.createStatement();
			int temp = stat.executeUpdate(sQuery);
			stat.close();
		}	catch(Exception e) {
			System.out.println("Insert Error!");
		}
		
	}
	
	public static boolean update(String sQuery)	{
		try {
			Statement stat = con.createStatement();
			int temp = stat.executeUpdate(sQuery);
			stat.close();
			return true;
		}	catch(Exception e)	{
			e.printStackTrace();
			System.out.println("Update Error");
		}
		return false; //if program reaches here, then update statement was false.
	}
	
	public static boolean isClosed() {
		try {
			return con.isClosed();
		} catch(Exception e) {}
		return false;
	}
	
	public static void close()	{
		try{
			con.close();
		}catch(Exception e){}
	}
	
	/**
	 * Allows the creation of a more customized SQL statement.
	 *
	 * Customizes the SQL statement settings according to the parameters.
	 */
	public static Statement createStatement(int nResultSetType, int nResultSetConcurrency) {
		try {
			return con.createStatement(nResultSetType, nResultSetConcurrency);
		} catch(Exception e) {}		
		return null;
	}
	
	/**
	 * Toggles the autocommit state to the specified flag parameter.
	 *
	 * Should be used for multiple sql statements that must be treated
	 * atomically.
	 */
	public static void setAutoCommit (boolean isOnAutoCommit) {
		try {
			con.setAutoCommit(isOnAutoCommit);
		} catch(Exception e) {
			System.err.println("Error: Cannot change AutoCommit state");
		}
	}
	
	/**
	 * Commits changes manually.
	 * Should ONLY be used when autoCommit is turned off.
	 *
	 * @see setAutoCommit(boolean)
	 */
	public static void commit() {
		try {
			con.commit();
		} catch(Exception e) {}
	}
	
	/**
	 * Rolls back changes manually.
	 * Should ONLY be used when autoCommit is turned off.
	 *
	 * Automatically reactivates autoCommit after rollback.
	 *
	 * @see setAutoCommit(boolean)
	 */
	public static void rollback() {
		try {
			con.rollback();
			if(!con.getAutoCommit())
				con.setAutoCommit(true);
		} catch(Exception e) {}		
	}
}